package ffte.property

import spinal.core._
import ffte.default.config

/**
 * Global FFT configuration case class
 *
 * This class defines the global configuration parameters that affect FFT
 * behavior across the entire design. It provides a mechanism to apply
 * configuration settings to specific code scopes.
 *
 * Configuration Parameters:
 * - fftScaleFactor: Global scaling factor for FFT computations
 *
 * Usage Pattern:
 * - Configurations can be applied to specific code blocks using .on{} or .apply{}
 * - Uses SpinalHDL's ScopeProperty for hierarchical configuration management
 * - Default values are provided by the global configuration system
 *
 * @param fftScaleFactor Global scaling factor applied to FFT computations
 */
case class FFTGlobeConfig(
    fftScaleFactor : Double
){

    /**
     * Apply configuration to a code block
     *
     * Sets this configuration as active within the specified code block
     * and restores the previous configuration afterward.
     *
     * @param body Code block to execute with this configuration
     * @tparam B Return type of the code block
     * @return Result of executing the code block with this configuration
     */
    def on[B](body : => B) = {
        FFTGlobeProperty(this) on {
            body
        }
    }

    /**
     * Alternative syntax for configuration application
     *
     * Provides a more natural function-call syntax for applying
     * configuration to a code block.
     *
     * @param body Code block to execute with this configuration
     * @tparam B Return type of the code block
     * @return Result of executing the code block with this configuration
     */
    def apply[B](body : => B): B = on(body)
}

/**
 * Global FFT configuration property manager
 *
 * This object extends SpinalHDL's ScopeProperty to provide hierarchical
 * configuration management for FFT parameters. It allows configurations
 * to be applied to specific scopes and automatically restored when
 * exiting those scopes.
 *
 * Features:
 * - Hierarchical configuration with automatic restoration
 * - Default configuration fallback
 * - Thread-safe configuration management
 * - Integration with SpinalHDL's configuration system
 */
object FFTGlobeProperty extends ScopeProperty[FFTGlobeConfig] {

    /**
     * Default configuration fallback
     *
     * Provides the default configuration values when no specific
     * configuration is active in the current scope.
     */
    override def default: FFTGlobeConfig = config.DefaultFFTGlobeConfig
}

/**
 * FFT scale factor accessor
 *
 * This object provides access to the current FFT scale factor from the
 * active global configuration. It serves as a convenient interface
 * for retrieving the scaling factor in FFT computations.
 *
 * Usage:
 * ```scala
 * val scaleFactor = getFFTScalaFactor()
 * ```
 */
object getFFTScalaFactor {

    /**
     * Get the current FFT scale factor
     *
     * Retrieves the scale factor from the active global configuration.
     * If no specific configuration is active, returns the default value.
     *
     * @return Current FFT scale factor as Double
     */
    def apply(): Double = FFTGlobeProperty.get.fftScaleFactor
}
